package com.lb.bs.client.serialize;

import com.lb.bs.client.adapter.enums.SerializeType;
import com.lb.bs.client.config.StaticConfig;
import com.lb.bs.client.repository.BeanHolder;

/**
 * program: fianl
 * author: bsworld.xie
 * create: 2020-03-18 18:39
 * description:
 */
public class SerializationHolder {
    private static volatile SerializationInterface serializationInterface;

    public static SerializationInterface getHolder() {

        if (serializationInterface == null) {
            synchronized (SerializationHolder.class) {
                if (serializationInterface == null) {
                    StaticConfig staticConfig = BeanHolder.getHolder().getBean(StaticConfig.class);
                    if (staticConfig == null) {
                        throw new IllegalStateException("SerializationHolder.class, getHolder(), staticConfig is null");
                    }
                    SerializeType serializeType = staticConfig.getSerializeType();
                    if (serializeType == null) {
                        serializeType = SerializeType.JDK;
                    }
                    if (SerializeType.JDK.equals(serializeType)) {
                        serializationInterface = new JdkSerialization();
                    } else if (SerializeType.COMMON.equals(serializeType)) {
                        serializationInterface = new CommonSerialization();
                    } else {
                        throw new IllegalStateException("erializationHolder.class, getHolder(),staticConfig serialize type is illegal");
                    }
                    BeanHolder.<SerializationInterface>getHolder().registerBean(SerializationInterface.class, serializationInterface);
                }

            }
        }

        return serializationInterface;
    }

    private static void main(String[] args) throws Exception {
        byte[] dataTrue = {-84, -19, 0, 5, 115, 114, 0, 17, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 66, 111, 111, 108, 101, 97, 110, -51, 32, 114, -128, -43, -100, -6, -18, 2, 0, 1, 90, 0, 5, 118, 97, 108, 117, 101, 120, 112, 1};
        byte[] dataFalse = {-84, -19, 0, 5, 115, 114, 0, 17, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 66, 111, 111, 108, 101, 97, 110, -51, 32, 114, -128, -43, -100, -6, -18, 2, 0, 1, 90, 0, 5, 118, 97, 108, 117, 101, 120, 112, 0};

        byte[] testStr = {-84, -19, 0, 5, 116, 0, 1, 73};

        String o = new JdkSerialization().deserialize(dataTrue);
        System.out.println(o);

        Object a = true;

        System.out.println((String)a);
//        Boolean deserialize = deserialize(dataTrue);
        //
//        System.out.println(deserialize);
//        Boolean deserialize21 = deserialize(dataFalse);
//        System.out.println(deserialize21);
//        String deserialize = new CommonSerializationUtil().deserialize(dataTrue);
//        System.out.println(deserialize);
    }

}
